--成长好礼

--#include "data\config\welfare\UpgradeGiftConfig.lua" once

function IsUpgradeGiftOpen(sysarg)
   return Actor.checkActorLevel(sysarg, UpgradeGiftCfg.OpenLevel[2], UpgradeGiftCfg.OpenLevel[1])
end

--广播次数
function BroadUpgradeGiftCount(sysarg)
   if not IsUpgradeGiftOpen(sysarg) then
      return
   end
   local players = LuaHelp.getAllActorList()
   if players then   
      for k, v in ipairs(players) do   
         if v == sysarg then
         else
            local npack = DataPack.allocPacket(v,LogicCmd.trSys.sysId,LogicCmd.trSys.sub.sBroadUpgradeGiftCount)  --申请一个数据包
            if (npack == nil) then
               --print("npack:nil")
               return
            end
            local ssVar = System.getStaticVar()
            if not ssVar.upGradeGiftCount then
               ssVar.upGradeGiftCount = 0   
            end
            local lastCount = UpgradeGiftCfg.TotalCount - ssVar.upGradeGiftCount
            DataPack.writeShort(npack, lastCount)
            DataPack.flush(npack)   --发送数据包
         end
      end
   end
end

--发送协议
function SendUpgradeGiftData(sysarg)
   if not IsUpgradeGiftOpen(sysarg) then
      return
   end

   local ssVar = System.getStaticVar()
   if not ssVar.upGradeGiftCount then
      ssVar.upGradeGiftCount = 0   
   end

   local npack = DataPack.allocPacket(sysarg,LogicCmd.trSys.sysId,LogicCmd.trSys.sub.sGetUpgradeGiftData)  --申请一个数据包
   if (npack == nil) then
      --print("npack:nil")
      return
   end

   local sVar = Actor.getStaticVar(sysarg)
   if not sVar.upgradeGiftState then
      sVar.upgradeGiftState = {}   
   end

   local lastCount = UpgradeGiftCfg.TotalCount - ssVar.upGradeGiftCount
   local buyState = sVar.upgradeGiftBuy or 0
   --print("buyState ="..buyState)
   --print("lastCount ="..lastCount)
   DataPack.writeByte(npack, buyState)
   DataPack.writeShort(npack, lastCount)
   local len = #UpgradeGiftCfg.Awards
   DataPack.writeByte(npack, len)
   for idx = 1, len do
      local state = sVar.upgradeGiftState[idx] or 0
      DataPack.writeByte(npack, state)
   end

   DataPack.flush(npack)   --发送数据包
end

--购买成长好礼
function BuyUpgradeGift(sysarg)
   if not IsUpgradeGiftOpen(sysarg) then
      return
   end
   local sVar = Actor.getStaticVar(sysarg)
   local myVipLevel = Actor.getVipGrade(sysarg)
   if myVipLevel < UpgradeGiftCfg.NeedVipLevel then
      Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.UpgradeGift002, UpgradeGiftCfg.NeedVipLevel), ttFlyTip)
      return
   end
   local days = System.getDaysSinceOpenServer()
    if days > UpgradeGiftCfg.DayLimit then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.UpgradeGift009, ttFlyTip)
        return
    end
   local ssVar = System.getStaticVar()
   if not ssVar.upGradeGiftCount then
      ssVar.upGradeGiftCount = 0   
   end
   if sVar.upgradeGiftBuy then
      Actor.sendTipmsg(sysarg, Lang.ScriptTips.UpgradeGift004, ttFlyTip)
      return
   end
   local lastCount = UpgradeGiftCfg.TotalCount - ssVar.upGradeGiftCount
   if lastCount <= 0 then
     --不限制
     -- Actor.sendTipmsg(sysarg, Lang.ScriptTips.UpgradeGift003, ttFlyTip)
     --return
   end

   if Actor.getMoneyCount(sysarg, mtYuanbao) < UpgradeGiftCfg.GoldNum then
      Actor.sendItemLack( sysarg, qatYuanbao, 0, UpgradeGiftCfg.GoldNum )
      return
   end

   if not Actor.delMoney( sysarg,mtYuanbao,UpgradeGiftCfg.GoldNum,LogIds.UpgradeGiftLogId,Lang.LogTips.UpgradeGift) then
      return
   end
   if ssVar.upGradeGiftCount < UpgradeGiftCfg.TotalCount then
     ssVar.upGradeGiftCount = ssVar.upGradeGiftCount + 1
   end
   BroadUpgradeGiftCount(sysarg)
   sVar.upgradeGiftBuy = 1
   local myLevel = Actor.getIntProperty(sysarg, PROP_CREATURE_LEVEL)
   local myCircle = Actor.getIntProperty(sysarg, PROP_ACTOR_CIRCLE)  
   UpgradeGiftLevelChange(sysarg, myCircle, myLevel, true)


   local sActorName = Actor.getName(sysarg)
   --print("sActorName ="..sActorName)
   --print("enLinkUpgradeGiftWnd ="..enLinkUpgradeGiftWnd)
   --print("Lang.ScriptTips.UpgradeGift001 = "..Lang.ScriptTips.UpgradeGift001)
   local msg = string.format(Lang.ScriptTips.UpgradeGift001, sActorName, enLinkUpgradeGiftWnd)
   System.broadcastTipmsg(msg,ttScreenCenter + ttChatWindow)
end

--领取成长好礼
function GetUpgradeGiftAward(sysarg, packet)
   if not IsUpgradeGiftOpen(sysarg) then
      return
   end

   local Index = DataPack.readByte(packet)
    
    
   
   if Index > #UpgradeGiftCfg.Awards then
      Actor.sendTipmsg(sysarg, Lang.ScriptTips.UpgradeGift005, ttFlyTip)
      return
   end

   local sVar = Actor.getStaticVar(sysarg)

    if not sVar.upgradeGiftState then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.UpgradeGift006, ttFlyTip)
        return
    end
    if not sVar.upgradeGiftState[Index] or sVar.upgradeGiftState[Index] ~= 1 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.UpgradeGift006, ttFlyTip)
        return
    end
    local rewards = UpgradeGiftCfg.Awards[Index].awards

    sVar.upgradeGiftState[Index] = 2
    
    if IsBagGridEnough(sysarg, rewards) then
        GiveCommonAward(sysarg, rewards, LogIds.UpgradeGiftLogId, Lang.LogTips.UpgradeGift)
    else
        local actorId = Actor.getActorId(sysarg)
        local title = Lang.ScriptTips.UpgradeGift007
        local content = Lang.ScriptTips.UpgradeGift008
        Awards.GiveToMailById(actorId, rewards, title, content, LogIds.UpgradeGiftLogId, Lang.LogTips.UpgradeGift)
    end

    SendUpgradeGiftData(sysarg)
end

--等级改变
function UpgradeGiftLevelChange(sysarg, circle, level, sendInfo)
   if not IsUpgradeGiftOpen(sysarg) then
      return
   end
   local sVar = Actor.getStaticVar(sysarg)
   if not sVar.upgradeGiftBuy then
      return
   end
   if not sVar.upgradeGiftState then
      sVar.upgradeGiftState = {}
   end
   local sendData = false
   if sendInfo then
      sendData = true
   end
   for k,v in pairs(UpgradeGiftCfg.Awards) do
      if Actor.checkActorLevel(sysarg, v.level, v.circle) then
         if sVar.upgradeGiftState[k] and sVar.upgradeGiftState[k] >= 1 then
         else
            sVar.upgradeGiftState[k] = 1
            sendData = true
         end
      end
   end

   if sendData then
      SendUpgradeGiftData(sysarg)
   end
end

--离线副本协议处理
UpgradeGiftMessageHandle = function (sysarg, systemid, msgid, packet)
    if msgid == LogicCmd.trSys.sub.cGetUpgradeGiftData then     
        SendUpgradeGiftData(sysarg)
    elseif msgid == LogicCmd.trSys.sub.cBuyUpgradeGift then 
        BuyUpgradeGift(sysarg)
    elseif msgid == LogicCmd.trSys.sub.cGetUpgradeGiftAward then
        GetUpgradeGiftAward(sysarg, packet)
    end
end